package com.example.robertluozizhao.framecollectdemo.adapter;

import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.robertluozizhao.framecollectdemo.R;
import com.example.robertluozizhao.framecollectdemo.adapter.holder.FlexViewHolder;
import com.google.android.flexbox.FlexboxLayoutManager;

import java.util.ArrayList;

/**
 * Created by robert.luozizhao on 2018/1/29.
 */

public class FlexRecycleAdapter extends RecyclerView.Adapter<FlexViewHolder>{


    private ArrayList<Integer> mData;

    public FlexRecycleAdapter(ArrayList<Integer> mData) {
        this.mData = mData;
    }

    public void upDataInfo(ArrayList<Integer> data){
        mData = data;
        notifyDataSetChanged();
    };

    @Override
    public FlexViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.flex_test, parent, false);

        // 实例化viewholder
        FlexViewHolder viewHolder = new FlexViewHolder(v);

        return viewHolder;
    }


    @Override
    public void onBindViewHolder(FlexViewHolder holder, int position) {
        //绑定数据
        TextView tvFlexTest = holder.getTvFlexTest();
        holder.setText(mData.get(position));
        ViewGroup.LayoutParams lp = tvFlexTest.getLayoutParams();
        if (lp instanceof FlexboxLayoutManager.LayoutParams) {
            FlexboxLayoutManager.LayoutParams flexboxLp =
                    (FlexboxLayoutManager.LayoutParams) tvFlexTest.getLayoutParams();
            flexboxLp.setFlexGrow(1.0f);
        }
    }

    @Override
    public int getItemCount() {
        if (mData!=null&&mData.size()>0) {
            return  mData.size();
        }
        return 0;
    }
}
